<?php

/*
 * This file is part of the Behat.
 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Behat\Behat\HelperContainer\ServiceContainer;

use Behat\Behat\Context\ServiceContainer\ContextExtension;
use Behat\Behat\HelperContainer\Argument\ServicesResolverFactory;
use Behat\Behat\HelperContainer\Call\Filter\ServicesResolver;
use Behat\Behat\HelperContainer\Exception\WrongServicesConfigurationException;
use Behat\Testwork\Call\ServiceContainer\CallExtension;
use Behat\Testwork\ServiceContainer\Extension;
use Behat\Testwork\ServiceContainer\ExtensionManager;
use Behat\Testwork\ServiceContainer\ServiceProcessor;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Behat helper container extension.
 *
 * Extends Behat with helper containers support.
 *
 * @author Konstantin Kudryashov <ever.zet@gmail.com>
 */
final class HelperContainerExtension implements Extension
{
    /*
     * Available extension points
     */
    public const HELPER_CONTAINER_TAG = 'helper_container.container';

    /**
     * @var ServiceProcessor
     */
    private $processor;

    /**
     * Initializes compiler pass.
     */
    public function __construct(?ServiceProcessor $processor = null)
    {
        $this->processor = $processor ?: new ServiceProcessor();
    }

    public function getConfigKey()
    {
        return 'helper_container';
    }

    public function initialize(ExtensionManager $extensionManager)
    {
    }

    public function configure(ArrayNodeDefinition $builder)
    {
    }

    public function load(ContainerBuilder $container, array $config)
    {
        $definition = new Definition(ServicesResolverFactory::class, [
            new Reference('service_container'),
        ]);
        $definition->addTag(ContextExtension::SUITE_SCOPED_RESOLVER_FACTORY_TAG, ['priority' => 0]);
        $container->setDefinition(ContextExtension::SUITE_SCOPED_RESOLVER_FACTORY_TAG . '.helper_container', $definition);

        $definition = new Definition(ServicesResolver::class);
        $definition->addTag(CallExtension::CALL_FILTER_TAG, ['priority' => 0]);
        $container->setDefinition(CallExtension::CALL_FILTER_TAG . '.helper_container', $definition);
    }

    public function process(ContainerBuilder $container): void
    {
        $references = $this->processor->findAndSortTaggedServices($container, self::HELPER_CONTAINER_TAG);

        foreach ($references as $reference) {
            if ($container->getDefinition((string) $reference)->isShared()) {
                throw new WrongServicesConfigurationException(sprintf(
                    'Container services must not be configured as shared, but `@%s` is.',
                    $reference
                ));
            }
        }
    }
}
